/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.setting;

import com.logical.allhit.store.StorageManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.EventListenerList;

/**
 *
 * @author Sanjeet Jha
 */
public class Setting implements Serializable {

    private static final Setting instance;
    private ArrayList<Account> accounts;
    private ArrayList<Proxy> proxys;
    private long sleepTime,proxyChangCount;
    private String userAgent, host;
    
    private EventListenerList listenerList = new EventListenerList();
    
    public void addSettingChandeEventListener(SettingChangeEventListener listener){
        listenerList.add(SettingChangeEventListener.class, listener);
    }
    
    public void removeSettingChandeEventListener(SettingChangeEventListener listener){
        listenerList.remove(SettingChangeEventListener.class, listener);
    }
    
    public void fireSettingChandeEvent(){
        SettingChangeEvent event = new SettingChangeEvent(instance);
        Object[] listeners = listenerList.getListenerList();
        for (int i=0; i<listeners.length; i+=2) {
            if (listeners[i]==SettingChangeEventListener.class) {
                ((SettingChangeEventListener)listeners[i+1]).settingChenged(event);
            }
        }
    }

    static {
        Setting init = null;
        try {
            init = (Setting) StorageManager.getInstance().retrieveSettings(
                    Setting.class.getSimpleName(), 
                    Setting.class.getClassLoader());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Setting.class.getName()).log(Level.WARNING, ex.getMessage());
        } catch (IOException e) {
            Logger.getLogger(Setting.class.getName()).log(Level.WARNING, e.getMessage());
        }
        if(init == null){
            init = new Setting();
        }
        instance = init;
    }
    
    public static Setting getInstance(){
        return  instance;
    }
    
    /**
     * Only called if not persisted
     */
    private Setting() {
        this.restoreDefaults();
    }

    public ArrayList<Account> getAccounts() {
        return accounts;
    }

    public void setAccounts(ArrayList<Account> accounts) {
        this.accounts = accounts;
    }

    public long getProxyChangCount() {
        return proxyChangCount;
    }

    public void setProxyChangCount(long proxyChangCount) {
        this.proxyChangCount = proxyChangCount;
    }
    
    public long getSleepTime() {
        return sleepTime;
    }

    public void setSleepTime(long sleepTime) {
        this.sleepTime = sleepTime;
    }

    public ArrayList<Proxy> getProxys() {
        return proxys;
    }

    public void setProxys(ArrayList<Proxy> proxys) {
        this.proxys = proxys;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
    
    

    private void restoreDefaults() {
        sleepTime = 1;
        proxyChangCount= 2;
        userAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1";
        host = "www.allhitdeals.com";
        accounts = new ArrayList<Account>();
        proxys = new ArrayList<Proxy>();
        Properties property = null;
        property = new Properties();
        try {
            property.load(new FileInputStream(new File(StorageManager.getInstance().getAppHome(), "abc.properties")));
        } catch (Exception ex) {
            Logger.getLogger(Setting.class.getName()).log(Level.WARNING, ex.getMessage());
        }
        int noofid = Integer.parseInt(property.getProperty("noofids"));
        for (int z = 1; z <= noofid; z++) {
            Account account = new Account();
            account.setName(property.getProperty("name" + z));
            account.setUid(Long.parseLong(property.getProperty("uid" + z)));
            account.setUsertype(property.getProperty("usertype" + z));
            account.setPassword(property.getProperty("uidpass" + z));
            accounts.add(account);
        }
    }
    
    public void save() {
        try {
            this.saveChecked();
        } catch (IOException ex) {
            Logger.getLogger(Setting.class.getName()).log(Level.WARNING, ex.getMessage());
        }

    }

    private void saveChecked() throws java.io.IOException {
        StorageManager.getInstance().saveSettings(this.getClass().getSimpleName(), this);
    }
}
